package pl.webapps.alarmme;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

public class PreferencesHandler {

    private static SharedPreferences mPreferences;
    private static final String PREF_ALARMS_STRING = "pref_alarms_string";
    private static final String PREF_RINGING_ALARM_TIME = "pref_ringing_alarm_time";
    private static final String PREF_SNOOZE_TIME_DELTA = "pref_snooze_time_delta";
    private static final String PREF_AUTO_SNOOZE_TIME_DELTA = "pref_auto_snooze_time_delta";
    private static final String PREF_ALARMS_SOUNDS_REPO = "pref_alarms_sounds_repo";
    private static final String PREF_ALARMS_STRING_SPLITTER = "&";
    private static final String PREF_ALARM_VOLUME = "pref_alarm_volume";
    private static final String PREF_RINGING_TIME = "pref_ringing_time";
    private static final String PREF_USE_MOTION_SENSOR = "pref_use_motion_sensor";
    private static final String PREF_SHOW_IN_NOTIFICATION = "pref_show_in_notification";

    public static void setSharedPreferences(SharedPreferences pref) {
        mPreferences = pref;
    }

    public static boolean saveCurrentAlarms(List<String> alarms) {
        if (alarms.size() == 0) {
            Editor e = mPreferences.edit();
            e.remove(PREF_ALARMS_STRING);
            e.commit();
            return true;
        }
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < alarms.size() - 1; ++i) {
            sb.append(alarms.get(i));
            sb.append(PREF_ALARMS_STRING_SPLITTER);
        }
        sb.append(alarms.get(alarms.size() - 1));
        Editor e = mPreferences.edit();
        e.remove(PREF_ALARMS_STRING);
        e.putString(PREF_ALARMS_STRING, sb.toString());
        e.commit();
        return true;
    }

    public static List<String> loadCurrentAlarms() {
        String alarmsString = mPreferences.getString(PREF_ALARMS_STRING, null);
        List<String> alarms = null;
        if (alarmsString != null && alarmsString.length() > 0) {
            String[] items = alarmsString.split(PREF_ALARMS_STRING_SPLITTER);
            alarms = new ArrayList<String>(Arrays.asList(items));
        }
        return alarms;
    }

    public static void saveRingingAlarm(String time) {
        Editor e = mPreferences.edit();
        e.remove(PREF_RINGING_ALARM_TIME);
        e.putString(PREF_RINGING_ALARM_TIME, time);
        e.commit();
    }

    public static void clearRingingAlarm() {
        Editor e = mPreferences.edit();
        e.remove(PREF_RINGING_ALARM_TIME);
        e.commit();
    }

    public static String loadRingingAlarm() {
        return mPreferences.getString(PREF_RINGING_ALARM_TIME, null);
    }

    public static void saveAlarmVolume(float volume) {
        Editor e = mPreferences.edit();
        e.remove(PREF_ALARM_VOLUME);
        e.putFloat(PREF_ALARM_VOLUME, volume);
        e.commit();
    }

    public static float loadAlarmVolume() {
        return mPreferences.getFloat(PREF_ALARM_VOLUME, 0.5f);
    }

    public static String getSongsRepo() {
        return mPreferences.getString(PREF_ALARMS_SOUNDS_REPO, "default");
    }

    public static void setSongsRepo(String repoName) {
        Editor e = mPreferences.edit();
        e.remove(PREF_ALARMS_SOUNDS_REPO);
        e.putString(PREF_ALARMS_SOUNDS_REPO, repoName);
        e.commit();
    }

    public static void saveSnoozeTimeDelta(int snooze) {
        Editor e = mPreferences.edit();
        e.remove(PREF_SNOOZE_TIME_DELTA);
        e.putInt(PREF_SNOOZE_TIME_DELTA, snooze);
        e.commit();
    }

    public static int loadSnoozeTimeDelta() {
        return mPreferences.getInt(PREF_SNOOZE_TIME_DELTA, RingingActivity.DEFAULT_SNOOZE_TIME);
    }

    public static void saveAutoSnoozeTimeDelta(int snooze) {
        Editor e = mPreferences.edit();
        e.remove(PREF_AUTO_SNOOZE_TIME_DELTA);
        e.putInt(PREF_AUTO_SNOOZE_TIME_DELTA, snooze);
        e.commit();
    }

    public static int loadAutoSnoozeTimeDelta() {
        return mPreferences.getInt(PREF_AUTO_SNOOZE_TIME_DELTA, RingingActivity.DEFAULT_AUTO_SNOOZE_TIME);
    }

    public static void saveRingingTime(int ringingSeconds) {
        Editor e = mPreferences.edit();
        e.remove(PREF_RINGING_TIME);
        e.putInt(PREF_RINGING_TIME, ringingSeconds);
        e.commit();
    }

    public static int loadRingingTime() {
        return mPreferences.getInt(PREF_RINGING_TIME, RingingActivity.DEFAULT_RINGING_TIME_SEC);
    }

    public static void saveUseMotionSensor(boolean useSensor) {
        Editor e = mPreferences.edit();
        e.remove(PREF_USE_MOTION_SENSOR);
        e.putBoolean(PREF_USE_MOTION_SENSOR, useSensor);
        e.commit();
    }

    public static boolean loadUseMotionSensor() {
        return mPreferences.getBoolean(PREF_USE_MOTION_SENSOR, false);
    }

    public static void saveShowInNotification(boolean showInNotification) {
        Editor e = mPreferences.edit();
        e.remove(PREF_SHOW_IN_NOTIFICATION);
        e.putBoolean(PREF_SHOW_IN_NOTIFICATION, showInNotification);
        e.commit();
    }

    public static boolean loadShowInNotification() {
        return mPreferences.getBoolean(PREF_SHOW_IN_NOTIFICATION, false);
    }
}
